package com.itheima.client;

import java.io.*;
import java.net.Socket;
import java.util.Properties;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        //1.创建Socket对象并绑定端口
        Socket socket = new Socket("127.0.0.1", 10001);
        System.out.println("服务器已经连接成功");

        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("==============欢迎来到黑马聊天室================");
            System.out.println("1登录");
            System.out.println("2注册");
            System.out.println("请输入您的选择：");
            String choose = sc.nextLine();
            switch (choose) {
                case "1" -> login(socket);
                case "2" -> rigister(socket);
                default -> System.out.println("没有这个选项！");
            }
        }
    }

    public static void login(Socket socket) throws IOException {
        //键盘录入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();

        //把用户名和密码拼接起来
        StringBuilder sb = new StringBuilder();
        //username=zhangsan&password=123
        sb.append("username=").append(username).append("&password=").append(password);

        //获取输出流
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        //第一次写出的是执行登录操作
        talk2Server(bw, "login");

        //第二次写出的是用户数据
        talk2Server(bw, sb.toString());

        //接收数据
        //获取输入流
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String message = br.readLine();
        System.out.println(message);
        //1：登录成功  2 密码有误   3 用户名不存在
        if ("1".equals(message)) {
            System.out.println("登录成功，开始聊天：");
            //开启一条单独的线程接收服务端端发送过来的信息
            new Thread(new ClientMyRunable(socket)).start();
            //开始聊天
            talk2All(bw);
        } else if ("2".equals(message)) {
            System.out.println("密码输入错误");
        } else if ("3".equals(message)) {
            System.out.println("用户名不存在");
        }
    }

    public static void rigister(Socket socket) throws IOException {
        //键盘录入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();

        //校验用户名
        if (soleUserName(username)) {
            System.out.println("用户名已经存在，请重新输入！");
            return;
        }
        String usernameRegex = "[a-zA-Z]{6,18}";
        if (!username.matches(usernameRegex)) {
            System.out.println("用户名不符合规则，请重新输入！");
            return;
        }

        System.out.println("请输入密码");
        String password = sc.nextLine();

        //校验密码
        String passwordRegex = "[a-zA-Z][0-9]{2,7}";
        if (!password.matches(passwordRegex)) {
            System.out.println("密码不符合规则，请重新输入！");
            return;
        }

        //把用户名和密码拼接起来
        StringBuilder sb = new StringBuilder();
        //username=zhangsan&password=123
        sb.append("username=").append(username).append("&password=").append(password);
        //获取输出流
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        //第一次写出的是执行注册操作
        talk2Server(bw, "register");

        //第二次写出的是用户数据
        talk2Server(bw, sb.toString());

        BufferedWriter bw1 = new BufferedWriter(new FileWriter("sockethomework\\servicedir\\userinfo.txt", true));
        bw1.write(username + "=" + password);
        bw1.newLine();
        bw1.flush();
        bw1.close();

        //开启一条单独的线程接收用户端发送过来的信息
        System.out.println("注册成功，开始聊天：");
        new Thread(new ClientMyRunable(socket)).start();
        talk2All(bw);
    }

    public static boolean soleUserName(String username) throws IOException {
        //2.先读取本地文件中所有的正确用户信息。
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream("sockethomework\\servicedir\\userinfo.txt");
        prop.load(fis);
        fis.close();
        if (prop.containsKey(username)) {
            return true;
        }
        return false;
    }

    //往服务器写出消息
    public static void talk2All(BufferedWriter bw) throws IOException {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入你要说的话：");
            String str = sc.nextLine();
            //把聊天内容写到服务器
            talk2Server(bw, str);
        }
    }

    //把聊天内容写到服务器
    public static void talk2Server(BufferedWriter bw, String content) throws IOException {
        bw.write(content);
        bw.newLine();
        bw.flush();
    }
}
